// 关于食物的
(function(window) {
    var list = [];
    // 食物类，用来实例化食物对象
    function Food(x, y, width, height, color) {
        this.x = x || 0;
        this.y = y || 0;
        this.width = width || 20;
        this.height = height || 20;
        this.color = color || 'green';
    }

    // 渲染食物的方法
    Food.prototype.render = function (map) {
        remove();
        this.x =  Math.floor(Math.random() * map.offsetWidth / this.width);
        this.y =  Math.floor(Math.random() * map.offsetHeight / this.height);
        var xw = this.x * this.width;
        var yh =  this.y * this.height;

        var div = document.createElement('div');
        div.style.position = 'absolute';
        div.style.width = this.width + 'px';
        div.style.height = this.height + 'px';
        div.style.backgroundColor = this.color;
        div.style.left = xw + 'px';
        div.style.top = yh + 'px';

        map.appendChild(div);
        list.push(div);
    }

    // 清除食物
    function remove () {
        for (var i = list.length - 1; i >= 0; i--) {
            list[i].parentNode.removeChild(list[i]);
            // 保存食物的数组里面也要删除div
            list.pop();
        }
    }

    window.Food = Food;
})(window);